package system;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

import ui.MainMenuUI;

public class BagOfWords extends Algorithm{
	
	
	public int m;
	public int n;
	
	
	public double[][] run(ArrayList<String> pathes,int numOfDocs){
		
		ArrayList<String> wordsVector;//dictionary 
		
		StringTokenizer st;
		BufferedReader br;
		String str;
	    int[] lineCounter = null;//number of words in book
		boolean flag=false;
		MainMenuUI.lblRunStatus.setText("BAG OF WORDS IN PROGRESS...");
		double temp;
		wordsVector= new ArrayList<String>();
		try {
			
			lineCounter= new int[pathes.size()];
			for(int i=0;i<pathes.size();i++){

			br = new BufferedReader(new FileReader(pathes.get(i)));
			
			    for(String line;(line = br.readLine()) != null;){
				    st = new StringTokenizer(line, " ");
				     lineCounter[i]++;
				         while (st.hasMoreElements()) {
				        	 
					       str=(String)st.nextElement();
					       
					       for(int j=0;j<wordsVector.size();j++){
					       if(wordsVector.get(j).equals(str))
					    	   flag=true;
					       }//end for
					       if(flag==false)
				    	   wordsVector.add(str);
					       else
					    	   flag=false;
				           
				         }//end while
			
				     	temp=0+(i/pathes.size())*30;
						MainMenuUI.progressBar.setValue((int)temp);
						
						MainMenuUI.ChangeProgressBar();
		        		
			    }//end for1
		
				
	        br.close();
	        
			}//end for2	
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Word vector creation failed! (BagOfWords)");
		}
		
		/////////////
	/*	for(int i=0;i<wordsVector.size();i++)
			System.out.println(wordsVector.get(i));*/
		
		////////////
		
		//Matrix X creation
		m= wordsVector.size();//rows
		n=numOfDocs*pathes.size();//cols
		
		
		double[][] X= new double[m][n];
		
		double[][] jWordInDoc=new double[m][n];
		double[] idfj=new double[m];
		double[] flagIdfj;
		double[] wordsInDoc=new double[n];
		String line;
		int j=0,l,k;
		int index=-1;
		int index1;
		int check=0;
		double[] flagIdfj1;
		
		//tij number of j word in document/total number of words in document
		//idfj	number of documents containing j word
		
		
		try {
		for(int i=0;i<pathes.size();i++){
			
			br = new BufferedReader(new FileReader(pathes.get(i)));
			check=lineCounter[i]/numOfDocs;
			for(k=0;k<numOfDocs-1;k++){
				 index++;
				///////
				// System.out.println("numner of doc: "+index);
				////////
				 flagIdfj1=new  double[m];
				
			     for(j=0;j<check  ;j++){
			    	 if((line = br.readLine()) != null){
			    	    st = new StringTokenizer(line, " ");
			    	    str=(String)st.nextElement();
			    	    index1=wordsVector.indexOf(str);
			    	    ///////
						// System.out.println(j+": "+str);
						////////
			    	    jWordInDoc[index1][index]++;
			    	    
			    	    if(flagIdfj1[index1]==0){
			    	    	idfj[index1]++;
			    	    	flagIdfj1[index1]=1;
			    	    		
			    	    }//if
			    	 }//if
			    	 else
			    		 break;
			    
			     }//for3
				 
			    
			     wordsInDoc[index]=j;
			    
		    	    
				 
				 
		      }//for2
			flagIdfj1=new  double[m];
			j=0;
			index++;
			
			while((line = br.readLine()) != null)
			{
		    
		    	    st = new StringTokenizer(line, " ");
		    	    str=(String)st.nextElement();
		    	    
		    	    index1=wordsVector.indexOf(str);
		    	    
					 j++;
		    	    jWordInDoc[index1][index]++;
		    	    
		    	    if(flagIdfj1[index1]==0){
		    	    	idfj[index1]++;
		    	    	flagIdfj1[index1]=1;
		    	    		
		    	    }//if
		    	
		      }//while
			 
		    
		     wordsInDoc[index]=j;
			 
		     br.close();

	    		temp=30.0+(i/pathes.size())*30;
				MainMenuUI.progressBar.setValue((int)temp);
		
		}//for1
		
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("X matrix creation fail (BagOfWords)!");
		}

	    double[] dist=new double[n];
		
		for(int i=0;i<n;i++){
			
			for(j=0;j<m;j++){
				
			//X[j][i]=tij*Math.log(n/idfj);
				
				X[j][i]=(jWordInDoc[j][i]/wordsInDoc[i])*Math.log(n/idfj[j]);
				
				dist[i]=dist[i]+(X[j][i])*(X[j][i]);
			}//for2
		    
			dist[i]=Math.sqrt(dist[i]);
			temp=60+(i+1/n)*30;
			MainMenuUI.progressBar.setValue((int)temp);
		
		}//for1
	
		
		//normalize x to Euclidian length
		for(int i=0;i<n;i++){
			for(j=0;j<m;j++){
			
		       if(dist[i]!=0)
				X[j][i]=X[j][i]/(dist[i]);
				
				
			}//for2
		    
		
			temp=90.0+(i+1/n)*10;
			MainMenuUI.progressBar.setValue((int)temp);
		}//for1
	
	MainMenuUI.lblRunStatus.setText("BAG OF WORDS COMPLETED!");
	
	return X;	
	}


	
}


